<template>
  <div class="choice-commidy">
    <div class="already-content">
      <EditTable
        ref="editTable"
        @onChangeSelectAll="onSelectAll"
        @onJoinSale="onJoinSale"
        @onJoinSaleCheck="onJoinSaleCheck"
        @onShowChild="onShowChild"
        :listData="columns1"
        :status="status"
        :list="data1"
      ></EditTable>
    </div>
  </div>
</template>
<script>
import EditTable from "../edit/PresentTable";
import { columns1 } from "./ChoicePreventMethods";
import { getValueArray, setEmptyArray, getSelectStatus, getValueStatus, compareSaleGift, compareOther } from "./ChoiceCommidyMethods"
import { getCode, setArrayValue, setAdd, isPriceEmpty } from "@/views/sales/common/TableSaleMethods"
export default {
  name: "choicecommidy",
  components: {
    EditTable
  },
  mounted() {
    this.getProductData({size: 5000});
  },
  data() {
    return {
      model1: "",
      value: "",
      cityList: [],
      columns1,
      data1: [],
      status: "loading"
    };
  },
  methods: {
    onShowChild(item) {
      item.showChildren = !item.showChildren;
    },
    getProductData(param) {
      let self = this;
      this.$ajaxGet("/activity/product/spu_sku_page", param).then(
        data => {
          self.status = "loading";
          self.$store.state.fullGiftSale.giftData = self.data1 = data.records;
          setAdd(self, self.data1, {index: "checked", val1: false, childName: "productSkuList"})
          setAdd(self, self.data1, {index: "showChildren", val1: false, childName: "productSkuList"})
          setAdd(self, self.data1, {index: "returnPrice", val1: "", childName: "productSkuList"})
          setAdd(self, self.data1, {index: "quantity", val1: "", childName: "productSkuList"})
          getCode(self, self.data1, {index: "parentcode",childName: "productSkuList"})
          self.status = "pass"
        }
      );
    },
    setAlert(params) {
      this.$Modal.confirm({
        title: "提醒",
        content: `${params}已经是赠品，赠品不能重复`,
        onOk: () => {
        },
        onCancel: () => {
        }
      });
    },
    getSaleData(type) {
      let text = ''
      switch(type) {
        case 1:
          text = '一阶赠品'
        break;
        case 2:
          text = '二阶赠品'
        break;
        case 3:
          text = '三阶赠品'
        break;
      }
      if(isPriceEmpty(this.$store.state.fullGiftSale.giftData, {index: "returnPrice", index1: "checked",childName: "productSkuList"})) {
        this.$Message.error(`${text}退货价格不能为空`)
        return false
      }
      if (type == 1) {
        let test = compareSaleGift(
          this.$store.state.fullGiftSale.giftData,
          this.$store.state.fullGiftSale.GetgiftData1,
          this.$store.state.fullGiftSale.GetgiftData2,
          this.$store.state.fullGiftSale.GetgiftData3
        );
        compareOther(
          this.$store.state.fullGiftSale.giftData,
          this.$store.state.fullGiftSale.GetgiftData2,
          this.$store.state.fullGiftSale.GetgiftData3,
          this.ChoiceGifts3,
          this.ChoiceGifts4
        )
        this.$store.commit("GetfgsgiftData1");
      } else if (type == 2) {
        let test1 = compareSaleGift(
          this.$store.state.fullGiftSale.giftData,
          this.$store.state.fullGiftSale.GetgiftData2,
          this.$store.state.fullGiftSale.GetgiftData1,
          this.$store.state.fullGiftSale.GetgiftData3
        );
        compareOther(
          this.$store.state.fullGiftSale.giftData,
          this.$store.state.fullGiftSale.GetgiftData1,
          this.$store.state.fullGiftSale.GetgiftData3,
          this.ChoiceGifts2,
          this.ChoiceGifts4
        )
        this.$store.commit("GetfgsgiftData2");
      } else if (type == 3) {
        let test2 = compareSaleGift(
          this.$store.state.fullGiftSale.giftData,
          this.$store.state.fullGiftSale.GetgiftData3,
          this.$store.state.fullGiftSale.GetgiftData1,
          this.$store.state.fullGiftSale.GetgiftData2
        );
        compareOther(
          this.$store.state.fullGiftSale.giftData,
          this.$store.state.fullGiftSale.GetgiftData1,
          this.$store.state.fullGiftSale.GetgiftData2,
          this.ChoiceGifts2,
          this.ChoiceGifts3
        )
        this.$store.commit("GetfgsgiftData3");
      }
    },
    onSelectAll(param) {
      //点击选中所有checked按钮的时候出发的事件
      param
        ? setArrayValue(this.data1, {
            index: "checked",
            val1: true,
            childName: "productSkuList"
          })
        : setArrayValue(this.data1, {
            index: "checked",
            val1: false,
            childName: "productSkuList"
          });
    },
    onJoinSaleBody(item, todo) {
      if (item.productSkuList && item.productSkuList.length > 0) {
        item.checked
          ? setArrayValue(item.productSkuList, {
              index: "checked",
              val1: true,
              childName: "productSkuList"
            })
          : setArrayValue(item.productSkuList, {
              index: "checked",
              val1: false,
              childName: "productSkuList"
            });
      } else if (!item.productSkuList) {
        /* 当是子集的时候，选择一个对应的父级就选上 */
        let status = false;
        if (!todo) {
          return false;
        }
        for (let child of todo.productSkuList) {
          if (child.checked) {
            status = true;
          }
        }
        if (!status) {
          //子集没有被选中
          todo.checked = false;
        } else {
          todo.checked = true;
        }
      }
    },
    onJoinSale(item, todo, selectAll) {
      //点击加入促销的时候触发的事件
      item.checked = !item.checked;
      this.onJoinSaleBody(item, todo);
    },
    onJoinSaleCheck(item, todo, selectAll) {
      // 点击checked按钮的时候触发的事件
      this.onJoinSaleBody(item, todo, {
        index: "checked",
        index1: "productSkuList"
      })
    }
  },
  computed: {
    ChoiceGifts2 () {
      return this.$store.getters.ChoiceGifts2
    },
    ChoiceGifts3 () {
      return this.$store.getters.ChoiceGifts3 || ''
    },
    ChoiceGifts4 () {
      return this.$store.getters.ChoiceGifts4  || ''
    }
  }
};
</script>

